﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OUSMS.AppLogic.DataAccessLayer;
using System.Web.ModelBinding;
using OUSMS.AppLogic.BusinessLogicLayer;
using OUSMS.Helpers;
namespace OUSMS
{
    public partial class PrejudicMotion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // id 参数应与控件上设置的 DataKeyNames 值匹配
        // 或用值提供程序特性装饰，例如 [QueryString]int id
        public Motions motionDetailFormView_GetItem([QueryString("MID")]long id)
        {
            MotionsBLL motionsBLL = new MotionsBLL();
            return motionsBLL.GetMotionsByID(id);
        }

        protected void filesGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label indexLbl = e.Row.FindControl("indexLbl") as Label;
                indexLbl.Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        // 返回类型可以更改为 IEnumerable，但是为了支持
        // 分页和排序，必须添加以下参数:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<MotionFiles> filesGridView_GetData([QueryString("MID")]long id)
        {
            MotionFilesBLL motionFilesBLL = new MotionFilesBLL();
            return motionFilesBLL.GetMotionFilesByMotionID(id);
        }

        protected void motionDetailFormView_ItemCreated(object sender, EventArgs e)
        {

        }

        protected void okBtn_Click(object sender, EventArgs e)
        {
            try
            {
                MotionsBLL motionsBLL = new MotionsBLL();
                Motions motionEntity = motionsBLL.GetMotionsByID(Convert.ToInt64(Request.QueryString["MID"]));
                motionEntity.PrejudicDateTime = DateTime.Now;
                motionEntity.PrejudicResult = "预审合格";
                TextBox commentTxtBox = motionDetailFormView.FindControl("commentTxtBox") as TextBox;
                if (string.IsNullOrEmpty(commentTxtBox.Text.Trim()))
                {
                    motionEntity.PrejudicComment = "预审合格";
                }
                else
                {

                    motionEntity.PrejudicComment = commentTxtBox.Text.Trim();
                }
                motionEntity.Status = "待转发";
                motionsBLL.Save();

                Response.Redirect("WaitForPrejudicMotionList.aspx");
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                Label errorMsgLbl = motionDetailFormView.FindControl("errorMsgLbl") as Label;
                errorMsgLbl.Text = string.Format("保存信息失败，失败原因：{0}，请报告管理员。", ex.Message);
                errorMsgLbl.Visible = true;
            }
        }

        protected void notOKbtn_Click(object sender, EventArgs e)
        {
            Label errorMsgLbl = motionDetailFormView.FindControl("errorMsgLbl") as Label;
            try
            {
                TextBox commentTxtBox = motionDetailFormView.FindControl("commentTxtBox") as TextBox;
                if (!string.IsNullOrEmpty(commentTxtBox.Text.Trim()))
                {
                    MotionsBLL motionsBLL = new MotionsBLL();
                    Motions motionEntity = motionsBLL.GetMotionsByID(Convert.ToInt64(Request.QueryString["MID"]));
                    motionEntity.PrejudicDateTime = DateTime.Now;
                    motionEntity.PrejudicResult = "预审不合格";
                    motionEntity.PrejudicComment = commentTxtBox.Text.Trim();
                    motionEntity.Status = "待提交";
                    motionsBLL.Save();
                    Response.Redirect("WaitForPrejudicMotionList.aspx");
                }
                else
                {
                    errorMsgLbl.Text = "请填写不合格原因，以便于申请者根据提示修改提案内容。";
                    errorMsgLbl.Visible = true;
                }                               
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
                
                errorMsgLbl.Text = string.Format("保存信息失败，失败原因：{0}，请报告管理员。", ex.Message);
                errorMsgLbl.Visible = true;
            }
        }
    }
}